<?php
    require_once dirname(__FILE__).'/../../class/rice.class.php';
    $cropID=$_GET["cropID"];
    $cropClass=new crop($cropID);
    $crop=$cropClass->getCrop();
    
    $riceObj=new rice($crop["rvID"]);
    $rice=$riceObj->getRice();
    $farmID=$crop["farmID"];
    
    $farmClass=new farm();
    $farmClass->setFarmID($farmID);
    $farm=$farmClass->getFarm();
    $memID=$farm["memID"];
    
    $memberClass=new member($memID);
    $member=$memberClass->getMember();
    
    $dateClass=new date();
    ?>
<style type="text/css">
    #structureContent{
        vertical-align: top;
    }
</style>
<div class="col-md-12" style="border-bottom: 1px solid #aaa;padding-top: 10px;padding-bottom: 10px;box-shadow: 0px 2px 5px #888;margin-bottom: 10px;">
    <?=$member["title"]." ".$member["firstName"]." ".$member["lastName"]?><br>
    <span style="font-size: 80%;">
        บันทึกต้นทุน แปลงนา <?php echo $farm["farmName"];?> <?=$farm["area"]?> ไร่
        <b><?=$rice["rvName"]?></b>
        <br>
        <?php
           $dateClass=new date();
           echo $dateClass->dateEncodeBE($crop["startDate"]);
           echo "-";
           echo $dateClass->dateEncodeBE($crop["endDate"]);
       ?>
    </span>
    <button class="btn btn-default" style="position: absolute;top: 0px; right: 0px;"
        onclick="
            window.open('?page=member/crop/addCropForm&farmID=<?=$farmID?>&cropID=<?=$cropID?>','_self');
        "
        >
        <span class="glyphicon glyphicon-pencil"></span> แก้ไข Crop
    </button>
</div>
<div style="display: inline-block;width: 100%;padding: 5px;">
    <div class="btn-group" style="float: right;">
        <button type="button" class="btn btn-default" style="float: left;"
            onclick="window.open('?page=member/crop/cropList&farmID=<?=$farmID?>','_self')"
            ><span class="glyphicon glyphicon-chevron-left"></span> เลือก Crop</button>
        <button type="button" class="btn btn-default" 
            onclick="window.open('?page=member/crop/cropCostInput&cropID=<?=$_GET["cropID"]?>','_self')"
            >บันทึกต้นทุน</button>
        <button type="button" class="btn btn-default active" 
            onclick="window.open('?page=member/crop/cropIncomeInput&cropID=<?=$_GET["cropID"]?>','_self')"
            >บันทึก รายรับ</button>
    </div>
</div>
<table style="width: 100%;">
    <tr>
        <td style="padding-top: 5px;padding-left: 10px;padding-right: 10px;">
            <form id="addIncomeForm" action="member/crop/cropIncomeSave.php" method="post" class="form-horizontal">
                <input type="hidden" name="cropID" value="<?=$cropID?>">
                <fieldset>
                    <?php
                        $queryIN="
                            select  *
                            from    income_list
                            where   parent is null
                            ";
                        $resultIN=$mysqli->query($queryIN);
                        $iIN=1;
                        while($rowIN=$resultIN->fetch_array(MYSQLI_ASSOC)){
                            ?>
                    <input type="hidden" name="incomeID[]" value="<?=$rowIN["incomeID"]?>">
                    <!-- Appended Input-->
                    <div class="form-group">
                        <label class="col-md-4 control-label" for="appendedtext"><?=$rowIN["incomeName"]?></label>
                        <div class="col-md-4">
                            <div class="input-group">
                                <input id="cost<?=$rowIN[incomeID]?>" name="income[]" class="form-control" placeholder="<?=$rowIN["incomeName"]?>" type="tel" 
                                    style="text-align: right;"
                                    value="<?=$cropClass->getIncome($rowIN["incomeID"])?>"
                                    >
                                <span class="input-group-addon">บาท/ไร่</span>
                            </div>
                            <button type="button" class="btn btn-default"
                                onclick="
                                    $('#calDIV').show();
                                    $('#calDIV').css('top',$(this).offset().top);
                                    $('#calDIV').css('left',$(this).offset().left+$(this).width()+50);
                                    $('#calTo').val('cost<?=$rowIN[incomeID]?>');
                                    $('#calInput').val('');
                                "
                                >ช่วยคำนวณ</button>
                        </div>
                    </div>
                                <?php
                            $iIN++;
                        }
                    ?>
                </fieldset>
            </form>
        </td>
    </tr>
</table>
<div id="calDIV" style="position: absolute;display: none;width: 200px;">
    <div class="input-group">
        <input type="hidden" id="calTo">
        <input id="calInput" name="cost[]" class="form-control" placeholder="กรอกต้นทุนรวม" type="tel" 
            style="text-align: right;min-width: 100px;"
            value="<?=$cropClass->getCost($rowMC["costID"])?>"
            onkeyup="
                var total = $(this).val()/<?=$farm["area"]?>;
                $('#'+$('#calTo').val()).val(total);
            "
            onblur="save();$('#calDIV').fadeOut()"
            >
        <!--<span class="input-group-addon">ต่อ <?=$farm["area"]?> ไร่</span>-->
        <span class="input-group-addon">บาท</span>
    </div>
</div>
<script type="text/javascript">
    $('input').change(function(){
        $.post('member/crop/cropIncomeSave.php',
            $('#addIncomeForm').serialize()
        ,function(data){
            
        });
    });
</script>